package ija.model.players;

import ija.model.types.FigureColor;
import ija.model.types.FigureDirection;

/**
 * 
 * @author Radek Necas, xnecas18
 */
public abstract class Player {

	protected String name;
	protected FigureColor color;
	protected FigureDirection direction;

	public Player() {
		this.name = "";
		this.color = FigureColor.UNKNOWN;
		this.direction = FigureDirection.NONE;
	}

	public Player(String name, FigureColor color, FigureDirection direction) {
		this.name = name;
		this.color = color;
		this.direction = direction;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setColor(FigureColor c) {
		this.color = c;
	}

	public void setDirection(FigureDirection d) {
		this.direction = d;
	}

	public String getName() {
		return this.name;
	}

	public FigureColor getColor() {
		return this.color;
	}

	public FigureDirection getDirection() {
		return this.direction;
	}

	@Override
	public String toString() {
		String s = "Name: " + this.getName() + " color: " + this.getColor().toString()
				+ " direction: " + this.getDirection().toString();
		s += "Type: player";
		return s;
	}

	public void checkDirectionUniqueness(Player[] players) {
		if (players.length >= FigureDirection.values().length) {
			// not enough colors
			// return;
		}

		for (Player p : players) {
			if ((p != null) && (p != this)) {
				if (this.getDirection() == p.getDirection()) {
					p.setUniqueDirection(players);
				}
			}
		}
	}

	public void checkColorUniqueness(Player[] players) {
		if (players.length >= FigureColor.values().length) {
			// not enough colors
			// return;
		}

		for (Player p : players) {
			if ((p != null) && (p != this)) {
				if (this.getColor() == p.getColor()) {
					p.setUniqueColor(players);
				}
			}
		}
	}

	public void setUniqueColor(Player[] players) {
		boolean Used;
		for (FigureColor fc : FigureColor.values()) {
			Used = false;
			for (Player p : players) {
				if (p.getColor() == fc) {
					Used = true;
					break;
				}
			}

			if (!Used) {
				this.setColor(fc);
				break;
			}
		}
	}

	public void setUniqueDirection(Player[] players) {
		boolean Used;
		for (FigureDirection fc : FigureDirection.values()) {
			Used = false;
			for (Player p : players) {
				if (p.getDirection() == fc) {
					Used = true;
					break;
				}
			}

			if (!Used) {
				this.setDirection(fc);
				break;
			}
		}
	}

	public void copyFrom(Player p) {
		this.setColor(p.getColor());
		this.setDirection(p.getDirection());
		this.setName(p.getName());
	}

	public abstract Player duplicate();
}
